package com.ui.basic.controller.administrative;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomBooleanEditor;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.common.basic.base.BaseController;
import com.common.basic.util.Globals;
import com.ui.basic.command.administrative.CommandCustomer;
import com.ui.basic.service.administrative.CustomerService;

@Controller
public class CustomerController extends BaseController {
	private static final transient org.apache.commons.logging.Log LOGGER = org.apache.commons.logging.LogFactory.getLog(CustomerController.class);
	
	@Autowired
	private CustomerService customerService;

	public void setCustomerService(CustomerService customerService) {
		this.customerService = customerService;
	}
	
	@RequestMapping(value = "/administrative/customer.page", method = RequestMethod.GET)
	public String page(
			final HttpServletRequest request,
			final HttpServletResponse response) {
		LOGGER.debug(">>>CustomerController.page()");

		//** security validation
		if (getRequestValidation(request, response)==null) {
			return null;
		}

		return "customer.page";
	}

	@RequestMapping(value = "/administrative/customer.view", method = RequestMethod.POST)
	public String view(Model model, HttpServletRequest request,
			HttpServletResponse response,
			@ModelAttribute("commandCustomer") CommandCustomer commandCustomer) {
		LOGGER.debug(">>>CustomerController.view");

		//** security validation
		if (getRequestValidation(request, response)==null) {
			return null;
		}

		try {
			customerService.setCombo(commandCustomer);
			model.addAttribute("commandCustomer", customerService.getCustomer(commandCustomer));
		} catch (SQLException e) {
			LOGGER.error(">>>Error :: " + e.getMessage());
			sendError(response, e.getMessage());
			return null;
		}
		return "customer.view";
	}
	
	@RequestMapping(value = "/administrative/customer.form", method = RequestMethod.POST)
	public String showForm(Model model, HttpServletRequest request,
			HttpServletResponse response,
			@ModelAttribute("commandCustomer") CommandCustomer commandCustomer) {
		LOGGER.debug(">>>CustomerController.showForm");
		return edit(model, request, response, commandCustomer);
	}
	
	@RequestMapping(value = "/administrative/customer.edit", method = RequestMethod.POST)
	public String edit(Model model, HttpServletRequest request,
			HttpServletResponse response,
			@ModelAttribute("commandCustomer") CommandCustomer commandCustomer) {
		LOGGER.debug(">>>CustomerController.edit");

		//** security validation
		if (getRequestValidation(request, response)==null) {
			return null;
		}

		try {
			customerService.setCombo(commandCustomer);
			if (commandCustomer.getId() != null)
				model.addAttribute("commandCustomer", customerService
						.getCustomerDetail(commandCustomer));
			else {
				model.addAttribute("commandCustomer", commandCustomer);
				commandCustomer.setStrAction(Globals.ACTION_ADD);
			}
		} catch (SQLException e) {
			LOGGER.error(">>>Error :: " + e.getMessage());
			sendError(response, e.getMessage());
			return null;
		}

		return "customer.edit";
	}

	@RequestMapping(value = "/administrative/customer.save", method = RequestMethod.POST)
	public String save(HttpServletRequest request,
			HttpServletResponse response,
			@ModelAttribute("commandCustomer") CommandCustomer commandCustomer,
			BindingResult result, Model model) {
		LOGGER.debug(">>>CustomerController.save");

		//** security validation
		if (getRequestValidation(request, response)==null) {
			return null;
		}

		validator.validate(commandCustomer, result);

		if (!hasFieldErrors(request, response, result)) {
			try {
				customerService.save(commandCustomer);
				return view(model, request, response, commandCustomer);
			} catch (Exception e) {
				if (commandCustomer.getId() != null)
					sendError(response, getMessage(request,
							"error.customerdata.saveFailed",
							new String[] { commandCustomer.getFirstname() + " "
									+ commandCustomer.getSurname()}));
				else
					sendError(response, getMessage(request,
							"error.customerdata.exist", new String[] { commandCustomer.getFirstname() + " "
							+ commandCustomer.getSurname() }));
				return null;
			}
		}
		return null;
	}
	
	@RequestMapping(value = "/administrative/customer.del", method = RequestMethod.POST)
	public String delete(Model model, HttpServletRequest request,
			HttpServletResponse response,
			@ModelAttribute("commandCustomer") CommandCustomer commandCustomer,
			BindingResult result) {
		LOGGER.debug(">>>CustomerController.delete");

		//** security validation
		if (getRequestValidation(request, response)==null) {
			return null;
		}

		if (commandCustomer.getId() != null
				|| (commandCustomer.getSelectedRecord() != null && commandCustomer
						.getSelectedRecord().length > 0)) {
			try {
				customerService.delete(commandCustomer);
				return view(model, request, response, commandCustomer);
			} catch (Exception e) {
				sendError(response, e.getMessage());
				return null;
			}
		}
		sendError(response, getMessage(request, "label.noDataSelected"));
		return null;
	}


	@InitBinder
	public void initBinder(WebDataBinder binder) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
		dateFormat.setLenient(false);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(
				dateFormat, false));
		binder.registerCustomEditor(Boolean.class, new CustomBooleanEditor(
				"true", "false", false));
	}

}